package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.*;

import java.util.List;
import java.util.Map;


@RestController
@Slf4j
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    //任务4
    @PostMapping("/upload")
    public Result upload(@RequestParam(value = "excelFile") MultipartFile file){
        log.info("[预约设置-文件上传批量],file:{}",file.getOriginalFilename());
        ordersettingService.upload(file);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }


    /**
     * 根据月份查询
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam Integer year,@RequestParam Integer month){
        List<OrderSetting> orderSettings = ordersettingService.getOrderSettingByMonth(year,month);
        List<Map<String,Object>> voList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderSetting.getOrderDate());
            Map<String ,Object> map = new HashMap<>();
            // 设置日期第几天
            map.put("date",cal.get(Calendar.DAY_OF_MONTH));
            // 设置当天最大人数
            map.put("number",orderSetting.getNumber());
            // 设置已经预约的人数
            map.put("reservations",orderSetting.getReservations());
            voList.add(map);
        }
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,voList);
    }

    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        log.info("[预约设置,根据日期编辑]data:{}",orderSetting);
        ordersettingService.editNumberByDate(orderSetting.getOrderDate(),orderSetting.getNumber());
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);

    }

//    @GetMapping("/getOrderSettingByMonth")
//    public Result getOrderSettingByMonth(String year, String month) {
//        try {
//            List<Map> list = ordersettingService.getOrderSettingByMonth(year,month);
//            //获取预约设置数据成功
//            return new Result(true, MessageConst.ACTION_SUCCESS,list);
//        }catch (Exception e){
//            e.printStackTrace();
//            //获取预约设置数据失败
//            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
//        }
//    }


}
